package com.dfbz.controller;

import com.dfbz.entity.User;
import com.dfbz.service.DeptService;
import com.dfbz.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@WebServlet("/dept/*")
public class DeptServlet extends BaseServlet {
    private DeptService deptService = new DeptService();
    private UserService userService = new UserService();

    /**
     * 获取所有部门信息及部门下的所属员工
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void findDept(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Map<String,Object>> listMap = deptService.findAll();
        for (Map<String, Object> map : listMap) {
            Long id = Long.parseLong(map.get("id").toString()) ;
            List<User> userList = userService.findUserByDeptId(id);
            if(userList == null || userList.size() == 0){
                map.put("deptCount",0);
            }
            map.put("userList",userList);
        }
        request.setAttribute("listMap",listMap);
        request.getRequestDispatcher("/html/department.jsp").forward(request,response);
    }
}
